﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marmat.Forms;
using Marmat.Forms.Skin;
using Marmat.Foundation;

namespace Marmat.Template.WinForm
{

    /// <summary>
    /// 框架默认登录窗体
    /// </summary>
    public partial class DefaultLoginForm : NoneBorderForm, ILoginForm
    {

        /// <summary>
        /// 构建 Marmat.Template.WinForm.DefaultLoginForm 类的新实例。
        /// </summary>
        protected DefaultLoginForm() : this(null)
        {
        }

        /// <summary>
        /// 构建 Marmat.Template.WinForm.DefaultLoginForm 类的新实例。
        /// </summary>
        /// <param name="login"></param>
        public DefaultLoginForm(ILogin login)
        {
            InitializeComponent();
            this.CompanyName = Application.CompanyName;
            this.ProductName = Application.ProductName;
            this.login = login;
            this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
            this.DragParentForm();
            this.panel1.DragParentForm();
            this.label4.DragParentForm();
            this.label5.DragParentForm();
            this.label6.DragParentForm();
            this.TrompTitle.DragParentForm();
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        protected ILogin login { get; }

        /// <summary>
        /// 获取返回的用户ID。
        /// </summary>
        protected string UserID { get; set; }

        /// <summary>
        /// 显示登录窗体
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public bool ShowDialog(out string user_id)
        {
            user_id = default;
            var dialog = this.ShowDialog();
            bool result = dialog == DialogResult.OK;
            if (result)
            {
                user_id = this.UserID;
                if (this.checkBox1.Checked)
                {
                    Properties.Settings.Default.PassWord = this.PassWord.Value.Trim();
                    Properties.Settings.Default.UserName = this.UserName.Value.Trim();
                }
                Properties.Settings.Default.RememberPassWord = this.checkBox1.Checked;
                Properties.Settings.Default.Save();
            }
            return result;
        }

        /// <summary>
        /// 获取与该应用程序关联的公司名称。
        /// </summary>
        [DefaultValue("")]
        [Description("获取与该应用程序关联的公司名称。")]
        protected new virtual string CompanyName
        {
            get => _companyName;
            set
            {
                this._companyName = value;
                this.Invalidate();
            }
        }
        private string _companyName;

        /// <summary>
        /// 获取与该应用程序关联的产品名称。
        /// </summary>
        [DefaultValue("")]
        [Description("获取与该应用程序关联的产品名称。")]
        public new virtual string ProductName
        {
            get => this._productName;
            set
            {
                this._productName = value;
                this.Invalidate();
            }
        }
        private string _productName;

        #region 绘制代码

        /// <summary>
        /// 窗体绘制事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DefaultLoginForm_Paint(object sender, PaintEventArgs e)
        {
            //var rec = new Rectangle(0, 0, 657, 543);
            //e.Graphics.DrawImage(Properties.Resources.loginForm_default, rec);

            var titleFont = new Font("幼圆", 26.25F, FontStyle.Regular);
            var titleRec = new Rectangle(0, 186, this.BackgroundImage.Width, titleFont.Height);
            TextRenderer.DrawText(e.Graphics, this.ProductName, titleFont, titleRec, Color.White, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);

            var secondaryTitle = new Font("幼圆", 14.25F, FontStyle.Regular);
            var secondaryRec = new Rectangle(0, 241, this.BackgroundImage.Width, secondaryTitle.Height);
            TextRenderer.DrawText(e.Graphics, this.CompanyName, secondaryTitle, secondaryRec, Color.White, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
        }

        //登录按钮颜色
        private Color loginBtnColor = Color.FromArgb(68, 75, 94);//默认色

        //鼠标进入
        private void label7_MouseEnter(object sender, EventArgs e)
        {
            this.loginBtnColor = Color.FromArgb(108, 115, 134);//焦点色
            this.label7.Invalidate();
        }

        //鼠标移出
        private void label7_MouseLeave(object sender, EventArgs e)
        {
            this.loginBtnColor = Color.FromArgb(68, 75, 94);//默认色
            this.label7.Invalidate();
        }

        //鼠标按下时
        private void label7_MouseDown(object sender, MouseEventArgs e)
        {
            this.loginBtnColor = Color.FromArgb(80, 90, 160);//紫色
            this.label7.Invalidate();
        }

        //鼠标释放时
        private void label7_MouseUp(object sender, MouseEventArgs e)
        {
            this.loginBtnColor = Color.FromArgb(68, 75, 94);//默认色
            this.label7.Invalidate();
        }

        private void label7_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var rect = new Rectangle(0, 0, this.label7.Width - 1, this.label7.Height - 1);
            var path = rect.GetRoundPath(7);
            using (var brush = new SolidBrush(loginBtnColor))
                e.Graphics.FillPath(brush, path);
            var font = new Font("幼圆", 15F, FontStyle.Regular);
            TextRenderer.DrawText(e.Graphics, "登录", font, rect, Color.White, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        }

        private async void DefaultLoginForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) await this.OnLogin();
        }

        #endregion




        private async void label7_Click(object sender, EventArgs e)
        {
            await this.OnLogin();
        }

        protected async Task OnLogin()
        {
            this.TrompTitle.Text = null;
            string username = this.UserName.Value.Trim();
            string password = this.PassWord.Value.Trim();
            if (string.IsNullOrEmpty(username))
            {
                this.TrompTitle.Text = "请输入用户名";
                this.UserName.Focus();
            }

            else if (string.IsNullOrEmpty(password))
            {
                this.TrompTitle.Text = "请输入密码";
                this.PassWord.Focus();
            }
            else
            {
                if (this.login != null)
                {
                    try
                    {
                        this.panel1.Enabled = false;
                        this.TrompTitle.Text = "正在登录,请稍后...";
                        this.TrompTitle.ForeColor = Color.Purple;
                        Application.DoEvents();
                        this.UserID = await this.login.Login(username, password);
                        this.DialogResult = DialogResult.OK;
                    }
                    catch (Exception ex)
                    {
                        this.TrompTitle.ForeColor = Color.Red;
                        this.TrompTitle.Text = ex.GetBaseException().Message;
                    }
                    finally
                    {
                        this.panel1.Enabled = true;
                    }
                }
                else
                {
                    this.TrompTitle.Text = "没有配置 Login 服务。";
                }
            }
        }

        private void DefaultLoginForm_Load(object sender, EventArgs e)
        {
            this.UserName.Value = Properties.Settings.Default.UserName;
            var result = Properties.Settings.Default.RememberPassWord;
            if (result)
            {
                this.PassWord.Value = Properties.Settings.Default.PassWord;
                this.checkBox1.Checked = result;
            }
            if (string.IsNullOrEmpty(this.UserName.Value))
                this.UserName.Select();
            else
                this.PassWord.Select();
        }

    }
}
